/**
 * 
 * @param {*} mapId 元素Id
 * @param {*} latlng 经纬度
 * @param {*} zoom 缩放级别
 */
import _ from 'lodash'
export function initMap(mapId, latlng, zoom) {
    if (JSON.stringify(latlng) == '{}' || latlng == '' || latlng == undefined) {
        latlng = { lat: '34.351962', lng: '108.928098' } //西安市
    }
    const map = L.map(mapId, {
        center: [latlng.lat, latlng.lng], // 地图中心 纬度,经度
        zoom: zoom != '' ? zoom : 5, //缩放比列
        zoomControl: true, //禁用 + - 按钮
        doubleClickZoom: true, // 禁用双击放大
        attributionControl: false // 移除右下角leaflet标识
    });
    let name = L.tileLayer(
        //"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
        process.env.VUE_APP_MAP_API + '/tile/{z}/{x}/{y}.png',
    ).addTo(map);
    // return _.cloneDeep(map);
    return map
}

/**
 * 
 * @param {*} map 地图对象
 * @param {*} options 操作按钮
 */
export function addControlOptions(map, options) {
    if (options == null) (
        options = []
    )
    map.pm.addControls({
        position: "topleft",
        drawPolygon: options.includes('drawPolygon'), // 添加绘制多边形
        drawMarker: options.includes('drawMarker'), //添加按钮以绘制标记
        drawCircleMarker: options.includes('drawCircleMarker'), //添加按钮以绘制圆形标记
        drawPolyline: options.includes('drawPolyline'), //添加按钮绘制线条
        drawRectangle: options.includes('drawRectangle'), //添加按钮绘制矩形
        drawCircle: options.includes('drawCircle'), //  添加按钮绘制圆圈
        editMode: options.includes('editMode'), //  添加按钮编辑多边形
        dragMode: options.includes('dragMode'), //   添加按钮拖动多边形
        cutPolygon: options.includes('cutPolygon'), // 添加一个按钮以删除图层里面的部分内容
        removalMode: options.includes('removalMode') // 清除图层
    });

    // 设置绘制后的线条颜色等
    map.pm.setPathOptions({
        color: "orange",
        fillColor: "green",
        fillOpacity: 0.4
    });
    map.pm.setLang('zh'); //设置语言  en, de, it, ru, ro, es, fr, pt_br, zh , nl 
}

//自定义marker图标
export function createIcon(iconUrl) {
    var myIcon = L.icon({
        iconUrl: iconUrl,
        //shadowUrl: 'my-icon-shadow.png',
        iconSize: [25, 41],
        iconAnchor: [12, 41],
        popupAnchor: [1, -34],
        shadowSize: [41, 41],
        shadowAnchor: [22, 94]
    });
    return myIcon
}